package com.ansion.basedb.database;

import android.database.Cursor;

import androidx.annotation.NonNull;


import com.ansion.basedb.database.sql.DBOperation;
import com.ansion.basedb.database.sql.DBOperationCode;
import com.ansion.basedb.database.sql.DBTaskState;
import com.ansion.basedb.database.sql.SQLColumn;
import com.ansion.basedb.database.sql.SQLExecutor;
import com.ansion.basedb.database.sql.SQLRow;
import com.ansion.basedb.storage.StorageSdk;
import com.ansion.basetool.preconditions.Preconditions;
import com.ansion.log.VLog;

import java.util.ArrayList;
import java.util.List;


/**
 * created by zhaoyuntao
 * on 10/11/2023
 */
class SQLDBHelper {
    public static ISqlDataBase getAppDataBase() {
        return StorageSdk.getMainDB().getDatabase();
    }


    public static void insertOrReplace(@NonNull String tableName, @NonNull List<SQLRow> rows) {
        Preconditions.checkNotNull(rows);
        SQLExecutor.execute(new DBOperation(getAppDataBase(), "Insert", DBOperationCode.INSERT) {
            @Override
            public Cursor op(@NonNull ISqlDataBase sqLiteDatabase, DBTaskState state) {
                for (SQLRow row : rows) {
                    sqLiteDatabase.replace(tableName, null, row.getContentValues());
                }
                return null;
            }
        });
    }

    public static List<SQLRow> select(@NonNull String sql) {
        List<SQLRow> list = new ArrayList<>();
        SQLExecutor.execute(new DBOperation(getAppDataBase(), "Select", DBOperationCode.SELECT) {
            @Override
            public Cursor op(@NonNull ISqlDataBase sqLiteDatabase, DBTaskState state) {
                VLog.i("TAG_DB_SQLITE", "SQLDBHelper.select sql:" + sql);
                Cursor cursor = sqLiteDatabase.rawQuery(sql);
                int columnCount = cursor.getColumnCount();
                while (cursor.moveToNext()) {
                    SQLRow row = new SQLRow();
                    for (int i = 0; i < columnCount; i++) {
                        String columnName = cursor.getColumnName(i);
                        int columnIndex = cursor.getColumnIndex(columnName);
                        Object value;
                        if (SQLColumn.NAME_COUNT.equalsIgnoreCase(columnName)) {
                            value = cursor.getInt(columnIndex);
                        } else {
                            //参照系统android.database.DatabaseUtils.cursorRowToContentValues是blob的时候按照blob去取，其他场景都按照string去取
                            if (cursor.getType(columnIndex) == Cursor.FIELD_TYPE_BLOB) {
                                value = cursor.getBlob(columnIndex);
                            } else {
                                value = cursor.getString(columnIndex);
                            }
                        }
                        row.put(columnName, value);
                    }
                    list.add(row);
                }
                return cursor;
            }
        });
        return list;
    }


}
